<style scoped>

</style>
<template>
    <div>
        <div class="top-title bg-white">
            <div class="top-title-text text-18 black blod">{{type}}</div>
        </div>
        <div class="page-content flex-column" style="height: calc(100% - 50px);">
            <div class="search-block bg-white p-20">
                <Form inline :label-width="90">
                    <FormItem label="名称/编码">
                        <Input v-model="search.str" class="w240" type="text" placeholder="请输入名称或编码"/>
                    </FormItem>
                    <!--<FormItem label=" " :label-width="20">-->
                        <!--<Select v-model="search.progress" class="w160"placeholder="方案" clearable>-->
                            <!--<Option value="1">已制定方案</Option>-->
                            <!--<Option value="2">未制定方案</Option>-->
                        <!--</Select>-->
                    <!--</FormItem>-->
                    <FormItem :label-width="0">
                        <Button @click="goSearch" class="m-r-10" type="primary">搜索</Button>
                        <Button @click="reset">重置</Button>
                    </FormItem>
                    <FormItem class="f-r" :label-width="0">
                        <Button @click="addItem" class="m-r-10" type="primary"><img src="assets/image/add.png">新增</Button>
                    </FormItem>
                </Form>
            </div>

            <div class="content-block bg-white p-0-20 flex-1 flex-column">

                <div id="mainContent" class="flex-1-height">

                    <div class="">
                        <Table :height="tableHeight"
                               :columns="columns"
                               :data="list"
                        >
                        </Table>
                    </div>

                </div>
                <div class="page-box">
                    <Page :total="total"
                          :current="page"
                          show-sizer show-elevator show-total
                          @on-change="onPageChange"
                          @on-page-size-change="changePageSize"
                          :page-size="pageSize"
                    />
                </div>

            </div>
        </div>

        <Modal v-model="itemModal"
               :title="itemData.id? '编辑': '新增'"
               class-name="vertical-center-modal"
               width="800"
        >
            <div class="p-l-20">
                <div class="top-title-text text-18 black blod">基础信息</div>
                <div class="p-15-0 max-h500">
                    <Form label-position="top">

                        <Row>
                            <Col span="11">
                                <FormItem label="手坏编码" required>
                                    <Input v-model="itemData.mj_mc" class="w240" type="text" placeholder="请输入"/>
                                </FormItem>
                            </Col>
                            <Col span="2" style="text-align: center"></Col>
                            <Col span="11">
                                <FormItem label="持有人" required>
                                    <Select v-model="itemData.wl_fzr" class="w240"placeholder="请选择" filterable clearable transfer>
                                        <Option v-for="(item,i) in userList"
                                                :key="i"
                                                :value="item.user_name"
                                        >{{item.user_name}}-{{item.phone_num}}</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>

                        <Row>
                            <Col span="11">
                                <FormItem label="手机号码" required>
                                    <Input v-model="itemData.sz_wz" class="w240" type="text" placeholder="请输入"/>
                                </FormItem>
                            </Col>
                            <Col span="2" style="text-align: center"></Col>
                            <Col span="11">
                                <FormItem label="发放时间" required>
                                    <Input v-model="itemData.mh" class="w240" type="text" placeholder="请输入"/>
                                </FormItem>
                            </Col>
                        </Row>

                    </Form>

                </div>
            </div>

            <div slot="footer">
                <Button @click="itemModal = false" class="m-r-10">取消</Button>
                <Button @click="confirm" type="primary">确定</Button>
            </div>
        </Modal>

    </div>
</template>

<script>

    export default {
        data() {
            return {
                type: '设备管理',

                search: {
                    str: '',
                    timeArr: [],
                },
                total: 0,
                page: 1,
                pageSize: 10,



                tableHeight: 500,
                columns: [
                    {
                        title: '手坏编码',
                        key: 'bm',
                    },
                    {
                        title: '使用人',
                        key: 'syr'
                    },
                    {
                        title: '电话号码',
                        key: 'sjh'
                    },
                    {
                        title: '状态',
                        key: 'zt'
                    },
                    {
                        title: '操作',
                        render: (h, params) => {
                            let look = h('a', {
                                class: 'm-r-10',
                                on: {
                                    click: () => {
                                        console.log('查看', params.row);
                                        this.$router.push({
                                            path: `/iotManage/doorDetail?type=${this.type}&id=${params.row.id}`
                                        })
                                    }
                                }
                            }, '查看');
                            let edit = h('a', {
                                class: 'm-r-10',
                                on: {
                                    click: () => {
                                        console.log('编辑', params.row);
                                        this.editItem(params.row);
                                    }
                                }
                            }, '编辑');
                            let del = h('a', {
                                class: 'red m-r-10',
                                on: {
                                    click: () => {
                                        console.log('删除', params.row);

                                        this.$Modal.confirm({
                                            title: '提示',
                                            content: '<p>确认删除吗？</p>',
                                            onOk: () => {
                                                let postData = {
                                                    mjId: params.row.id
                                                };

                                                this.util.post('/hand/deleteMj', postData, 'formdata').then(res => {
                                                    if(res.status == 200) {
                                                        this.$Message.success(res.msg);
                                                        this.getList();
                                                    } else {
                                                        this.$Message.warning(res.msg)
                                                    }
                                                })
                                            },
                                        });
                                    }
                                }
                            }, '删除');
                            return h('div', [edit, del]);
                        }
                    },
                ],
                list: [

                    {
                        bm: '863659048822117',
                        syr: '黄良仙',
                        sjh: '15296783113',
                        zt: '正常',
                    },
                    {
                        bm: '2343243238',
                        syr: '胡勺勺',
                        sjh: '13028004550',
                        zt: '正常',
                    },


                ],

                itemModal: false,
                itemData: {
                    "mj_mc":"",
                    "mj_bh": "",
                    "sz_wz": "",
                    "mh": "",
                    "lxfs": "",
                    "ms": "",
                    "gly": "",
                },

                userList: [],
            }
        },
        created() {
            // this.getList();
            this.getUserList();
        },
        mounted() {
            let mainContent = document.getElementById('mainContent')
            if(mainContent) {
                this.tableHeight =  mainContent.offsetHeight - 40
            }
            this.timer = setInterval(() => {
                if(mainContent) {
                    this.tableHeight =  mainContent.offsetHeight - 40
                } else {
                    mainContent = document.getElementById('mainContent')
                }
            }, 1000)
        },
        beforeDestroy() {
            clearInterval(this.timer);
        },
        methods: {
            getUserList() {

                let postData = {
                    // orId: this.detailId,
                };
                this.util.get('/hand/userAccountLists', postData).then(res => {
                    console.log('获取机构人员', JSON.parse(JSON.stringify(res)))
                    res.data && res.data.forEach(v => {
                        v.state = v.status === 'T'

                        v.label = v.user_name;
                        v.description = v.phone_num;
                        v.key = v.id;
                    })
                    this.userList = res.data || []

                    this.targetList = this.userList.slice(0, 3).map(v => v.id)
                })
            },
            onPageChange(page) {
                this.page = page;
                this.getList();
            },
            //改变每页条数
            changePageSize(size) {
                this.pageSize = size;
                this.onPageChange(1)
            },
            goSearch() {
                this.onPageChange(1)
            },
            reset() {
                this.search = {
                    str: '',
                    timeArr: [],
                };
                this.goSearch();
            },
            getList() {
                let postData = {
                    pageSize: this.pageSize,
                    pageNum: this.page,
                    nameOrCode: this.search.str,
                };

                if(this.search.timeArr.length) {
                    if(this.search.timeArr[0]) {
                        postData.startDate = moment(this.search.timeArr[0]).format('YYYY-MM-DD 00:00:00')
                    }
                    if(this.search.timeArr[1]) {
                        postData.endDate = moment(this.search.timeArr[1]).format('YYYY-MM-DD 23:59:59')
                    }
                }

                this.util.get('/hand/mjLists', postData).then(res => {
                    console.log('获取列表', JSON.parse(JSON.stringify(res)))
                    this.list = res.data || [];
                    this.total = res.total || 0;

                })
            },

            addItem() {
                this.itemData = {
                    "mj_mc":"",
                    "mj_bh": "",
                    "sz_wz": "",
                    "mh": "",
                    "lxfs": "",
                    "ms": "",
                    "gly": "",
                };
                this.itemModal = true;
            },
            editItem(itemData) {

                this.itemData = {
                    id: itemData.id,
                    "mj_mc": itemData.mj_mc,
                    "mj_bh": itemData.mj_bh,
                    "sz_wz": itemData.sz_wz,
                    "mh": itemData.mh,
                    "lxfs": itemData.lxfs,
                    "ms": itemData.ms,
                    "gly": itemData.gly,
                };
                this.itemModal = true;
            },
            confirm() {

                let itemData = this.itemData;

                if(!itemData.mj_mc) {
                    this.$Message.warning('请输入名称');
                    return;
                }
                if(!itemData.mj_bh) {
                    this.$Message.warning('请输入编号');
                    return;
                }
                if(!itemData.sz_wz) {
                    this.$Message.warning('请输入所在位置');
                    return;
                }
                if(!itemData.mh) {
                    this.$Message.warning('请输入门号');
                    return;
                }
                if(!itemData.gly) {
                    this.$Message.warning('请输入管理员');
                    return;
                }
                if(!itemData.lxfs) {
                    this.$Message.warning('请输入联系方式');
                    return;
                }

                let postData = {
                    "mj_mc": itemData.mj_mc,
                    "mj_bh": itemData.mj_bh,
                    "sz_wz": itemData.sz_wz,
                    "mh": itemData.mh,
                    "lxfs": itemData.lxfs,
                    "ms": itemData.ms,
                    "gly": itemData.gly,
                };

                let url = '/hand/editMj';
                if(itemData.id) {
                    postData.id = itemData.id;
                    url = '/hand/editMj';
                }

                this.util.post(url, postData).then(res => {
                    if(res.status == 200) {
                        this.$Message.success(res.msg);
                        this.getList();
                        this.itemModal = false;
                    } else {
                        this.$Message.warning(res.msg)
                    }
                })
            },

        },
    }
</script>

